package zheezes.eisp.autotask.model;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;

import zheezes.util.ConstantUtil;
import zheezes.util.DateFormatUtil;


public class ScheduleBean {
	public static final int VIP_METER_O_CLOCK_FRZ = 0;
	public static final int RES_METER_DAILY_FRZ = 1;
	public static final int SUM_METER_DAILY_FRZ = 2;
	public static final int SUM_METER_INSTANT_VALUE = 3;

	private Properties props = new Properties();
	private Map<Integer, String> map;

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public ScheduleBean() {
		Collection cc = new HashSet();
		cc.add(int.class);
		map = ConstantUtil.vmap(ScheduleBean.class, cc);
	}

	public int load(InputStream in) {
		try {
			props.load(in);
		} catch (IOException e) {
			return -1;
		}
		return 0;
	}
	
	public int save(OutputStream out) {
		try {
			props.store(out, null);
		} catch (IOException e) {
			return -1;
		}
		return 0;
	}

	public Calendar get(int type) {
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(DateFormatUtil.getDateFmt().parse((String) props.get(map.get(type).toLowerCase())));
		} catch (ParseException e) {
		}

		return cal;
	}

	public void set(int type, Calendar cal) {
		props.setProperty(map.get(type).toLowerCase(), DateFormatUtil.getDateFmt().format(cal.getTime()));
	}
}
